package com.shijie.controller.erp;

import com.shijie.component.redis.RedisClient;
import com.shijie.component.redis.RedisKey;
import com.shijie.service.ProductsService;
import com.shijie.domain.entity.Products;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/products")
public class ProductsController {

    @Resource
	private ProductsService productsService;

    @Autowired
    private RedisClient redisClient;

    @RequiresPermissions(value={"product:list"})
	@GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) String q
    		,@RequestParam(required=false) Integer status ,@RequestParam(required=false) Integer tradeType) {
        PageHelper.startPage(page, pageSize);
        List<Products> list = productsService.searchProducts(q,status,tradeType);
        PageInfo<Products> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
	}

    @Log(title = "产品管理", action = BusinessType.INSERT)
    @RequiresPermissions(value={"product:create","product:edit"})
	@PostMapping
    public Result add(@RequestBody Products products) {
        productsService.save(products);
        return ResultGenerator.success();
	}

    @RequiresPermissions(value={"product:delete"})
    @Log(title = "产品管理", action = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        productsService.delete(id);
        // 删除PV
        redisClient.removeZet(RedisKey.APP_PRODUCT_PV, id);
        return ResultGenerator.success();
    }
    
   	@GetMapping("getByIds")
       public Result getProductListById(@RequestParam String ids) {
   			ids = ids.substring(0,ids.lastIndexOf(","));
           List<Products> list = productsService.selectByIds(ids);
           return ResultGenerator.success(list);
   	}
    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Products products = productsService.selectById(id);
        return ResultGenerator.success(products);
	}
}

